package com.example.shiyan3.controller;

import com.example.shiyan3.model.RegionStats;
import com.example.shiyan3.service.DataService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Controller
public class levelController {

    @Autowired
    private DataService dataService;

    public levelController(){

    }

    @Autowired
    ObjectMapper objectMapper;

    @GetMapping(value = "/test")
    public ModelAndView test(Model model, String country) throws IOException {
        int totalReported = dataService.fetchCoronaVirusData().parallelStream().mapToInt(RegionStats::getLastcases).sum();
        int totalnewcases = dataService.fetchCoronaVirusData().parallelStream().mapToInt(RegionStats::getDifferentday).sum();
        model.addAttribute("totalReported", totalReported);
        model.addAttribute("totalnewcases", totalnewcases);
        List<RegionStats> iii=dataService.fetchCoronaVirusData()
                .parallelStream()
                .filter(locationStats -> locationStats.getCountry().equals(country))
                .collect(Collectors.toUnmodifiableList());

        if (country != null) {
            ModelAndView mv1 = new ModelAndView();
            mv1.addObject("list",iii);
            mv1.setViewName("index");
            return mv1;
        } else {
            ModelAndView mv = new ModelAndView();
            mv.addObject("list", dataService.fetchCoronaVirusData());
            mv.setViewName("index");
            return mv;
        }

    }

}
